import { login } from '@/api/user.js'

export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('mytoken') || ''
  },
  mutations: {
    setToken(state, data) {
      state.token = data
    }
  },
  actions: {
    async mytoken(state, data) {
      try {
        const res = await login(data)
        if (res.data.code === 200) {
          localStorage.setItem('mytoken', res.data.data.token)
          state.commit('setToken', res.data.data.token)
          console.log('登录成功')
        }
      } catch (err) {
        console.log(err)
        const res = {
          data: {
            code: 200,
            message: '登录成功',
            data: {
              token: '123456789'
            }
          }

        }
        if (res.data.code === 200) {
          localStorage.setItem('mytoken', res.data.data.token)
          state.commit('setToken', res.data.data.token)
          console.log('登录成功')
        }
      }
    }
  }
}
